package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.pojo.Dish;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @PostMapping
    public R<String>  addDish (@RequestBody DishDto dishDto){
        dishService.addDishWithFlavor(dishDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<DishDto>> pageDishDto(Integer page, Integer pageSize , String name){

        Page<Dish> pageInfo = new Page<>(page,pageSize);
        // 把Pige<Dish>转为Page<DishDto> 同时把categryName给塞进去
        Page<DishDto>  dishDtoPage = dishService.pageDish(page,pageSize,name);
        return  R.success(dishDtoPage);
    }

    @GetMapping("/{id}")
    public R<DishDto> getDish(@PathVariable Long id){
        DishDto dishDto = dishService.getDishWithFlavor(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> updateDish(@RequestBody  DishDto dishDto){
        dishService.updateDishWithFlavor(dishDto);
        return  R.success("修改成功");
    }
//    @PutMapping
//    public R<String> updateDish(@RequestBody DishDto dishDto){
//        dishService.updateDishWithFlavor(dishDto);
//        return R.success("修改成功");
//    }


    // 移动端显示菜品
    @GetMapping("/list")
    public R<List<DishDto>> getDishList(Long categoryId, String name , Integer status){
        List<DishDto> dishList = dishService.getDishList(categoryId,name,status);
        return R.success(dishList);
    }




    // 批量删除
    @DeleteMapping
    public R<String> deleteDish(@RequestParam List<Long> ids){
        log.info("ids: {}",ids);
        dishService.removeDishList(ids);
        return  R.success("删除成功");
    }

    //  批量起售
    @PostMapping("/status/1")
    public R<String> statusDish(@RequestParam List<Long> ids){
        // update Dish set status = 1  where id in (?,?,?)
        log.info("ids: {}",ids);
        dishService.updateDish(ids);
        return  R.success("起售成功");
    }


    // 批量停售
    @PostMapping("/status/0")
    public R<String> statusDish0(@RequestParam List<Long> ids){

        //update Dish set status = 0  where id in (?,?,?)
        dishService.statusDish0(ids);
        return R.success("停售成功");
    }


}
